package com.huangguolin.cloud.api.user.control;

import com.huangguolin.cloud.api.user.entity.ApiUserEntity;
import com.huangguolin.cloud.api.user.service.ApiUserService;
import com.huangguolin.cloud.mybatis.entity.PtIndividualEntity;
import com.huangguolin.cloud.mybatis.entity.PtUserEntity;
import com.huangguolin.cloud.mybatis.flexigrid.PagingEntity;
import com.huangguolin.cloud.spring.control.BaseController;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/api/user")
public class ApiUserController extends BaseController {
    @Resource
    private ApiUserService apiUserService = null;

    @Resource
    HttpSession session = null;

    private Logger logger = LogManager.getLogger(this.getClass());

    //登录
    @RequestMapping("login")
    public String login(String userName,String passWord){
        logger.info("userName:"+ userName + "passWord" + passWord);
        List<PtUserEntity> list = apiUserService.login(userName,passWord);
        if(list==null){
            return failJSON("login.fail");
        }
        return successJSONES("login.success",list);
    }


    //注册
    @RequestMapping("register")
    @ResponseBody
    public String register(String userName,String passWord,String identity,String phone,String email){
        apiUserService.register(userName,passWord,identity,phone,email);
        return successJSON("login.register.success");
    }

    //忘记密码
    @RequestMapping("forget")
    @ResponseBody
    public String forget(String userName,String passWord){
        apiUserService.forget(userName,passWord);
        return successJSON("login.email.restting.success");
    }

    //分页查询
    @RequestMapping("queryUserList")
    @ResponseBody
    public List<PtUserEntity> queryUserList(PagingEntity paging,ApiUserEntity entity){
        logger.info(paging);
        List<PtUserEntity> list = apiUserService.queryUserList(paging,entity);
        return list;
    }

    //查询总条数
    @RequestMapping("queryUserTotal")
    @ResponseBody
    public String queryUserTotal(ApiUserEntity entity){
        Integer total = apiUserService.queryUserTotal(entity);
        return totalJSON(total);
    }


    //发送验证码
    @RequestMapping("/sendemailregcode")
    @ResponseBody
    public String sendemailregcode(@RequestParam(value = "emailCode",required = false)String emailCode){

        apiUserService.getemailcode(emailCode);
        String code= (String) session.getAttribute("code");
        if(code==null){
            return failJSON("login.sendemailregcode.fail");
        }
        return successJSONS("login.sendemailcode.success",code);
    }

    //修改个人信息
    @RequestMapping("/personalInformation")
    @ResponseBody
    public String personalInformation(String userUuid,String userName,String gender,String birthday,String signature,String occupation,String firm,String hometown,String phone,String email,String personalInstructions) {
        apiUserService.personalInformation(userUuid,userName,gender,birthday,signature,occupation,firm,hometown,phone,email,personalInstructions);
        return successJSON("information.modified.successfully");
    }

    //查询用户uuid
    @RequestMapping("/query")
    @ResponseBody
    public String query(String userName,String passWord) {
        String userUuid = apiUserService.query(userName,passWord);
        if (userUuid==null){
            return failJSON("login.verify.fail");
        }
        return successJSONS("information.modified.successfully",userUuid);
    }



    //查询个人信息
    @RequestMapping("queryUser")
    @ResponseBody
    public List<PtIndividualEntity>queryUser(String userUuid){
        List<PtIndividualEntity> list = apiUserService.queryUser(userUuid);
        return list;
    }

    //修改个人信息
    @RequestMapping("/revise")
    @ResponseBody
    public String revise(String userUuid,String userName,String gender,String birthday,String signature,String occupation,String firm,String hometown,String phone,String email,String personalInstructions) {
        apiUserService.revise(userUuid,userName,gender,birthday,signature,occupation,firm,hometown,phone,email,personalInstructions);
        return successJSON("information.modified.successfully");
    }
    //修改教师个人信息
    @RequestMapping("/reviseTea")
    @ResponseBody
    public String reviseTea(String userName,String nickname,String password,Integer sex,Integer level,String phone,String email,String introduction,String photo) {
        apiUserService.reviseTea(userName,nickname,password,sex,level,phone,email,introduction,photo);
        return successJSON("information.modified.successfully");
    }
    //查询教师个人信息
    @RequestMapping("queryUserTea")
    @ResponseBody
    public List<PtUserEntity>queryUserTea(String phone){
        List<PtUserEntity> list = apiUserService.queryUserTea(phone);
        return list;
    }

    //删除用户
    @RequestMapping("/delete")
    public String delete(@RequestParam("userUuids") String userUuids){
        if (userUuids == null){
            return failJSON("user.delete.fail");
        }
        apiUserService.deleteUser(userUuids);
        return successJSON("user.delete.success");
    }
    //删除教师用户
    @RequestMapping("/deleteTea")
    public String deleteTea(@RequestParam("phone") String phone){
        if (phone == null){
            return failJSON("user.delete.fail");
        }
        apiUserService.deleteUserTea(phone);
        return successJSON("user.delete.success");
    }

    //分页查询教师信息
    @RequestMapping("queryUserTeacherList")
    @ResponseBody
    public List<PtUserEntity> queryTeacherList(PagingEntity paging,ApiUserEntity entity){
        logger.info(paging);
        List<PtUserEntity> list = apiUserService.queryUserTeacherList(paging,entity);
        return list;
    }

    //查询教师信息总条数
    @RequestMapping("queryUserTeacherTotal")
    @ResponseBody
    public String queryUserTeacherTotal(ApiUserEntity entity){
        Integer total = apiUserService.queryUserTeacherTotal(entity);
        return totalJSON(total);
    }
    //分页查询学生信息
    @RequestMapping("queryUserStudentList")
    @ResponseBody
    public List<PtUserEntity> queryUserStudentList(PagingEntity paging,ApiUserEntity entity){
        logger.info(paging);
        List<PtUserEntity> list = apiUserService.queryUserStudentList(paging,entity);
        return list;
    }
    //删除学生用户
    @RequestMapping("/deleteStu")
    public String deleteStu(@RequestParam("phone") String phone){
        if (phone == null){
            return failJSON("user.delete.fail");
        }
        apiUserService.deleteUserStu(phone);
        return successJSON("user.delete.success");
    }
    //查询学生信息总条数
    @RequestMapping("queryUserStudentTotal")
    @ResponseBody
    public String queryUserStudentTotal(ApiUserEntity entity){
        Integer total = apiUserService.queryUserStudentTotal(entity);
        return totalJSON(total);
    }
    //添加教师个人信息
    @RequestMapping("/AddUserTea")
    @ResponseBody
    public String AddUserTea(String userName,String nickname,String password,Integer sex,Integer level,Integer role,String phone,String email,String introduction,String photo) {
        apiUserService.AddUserTea(userName,nickname,password,sex,level,role,phone,email,introduction,photo);
        return successJSON("information.modified.successfully");
    }
    @RequestMapping("/PlusUser")
    @ResponseBody
    public String PlusUser(String userName,String nickName,String password,Integer id,String phone, String email,String qqNumber,String signature,String introduction) {
        apiUserService.PlusUser(userName,nickName,password,id,phone,email,qqNumber,introduction,signature);
        return successJSON("information.modified.successfully");
    }
}